package net.sunofbeach.blog.services.impl;

import net.sunofbeach.blog.dao.SettingDao;
import net.sunofbeach.blog.pojo.Setting;
import net.sunofbeach.blog.response.ResponseResult;
import net.sunofbeach.blog.services.IWebSizeInfoService;
import net.sunofbeach.blog.utils.Constants;
import net.sunofbeach.blog.utils.IdWorker;
import net.sunofbeach.blog.utils.RedisUtils;
import net.sunofbeach.blog.utils.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
@Transactional
public class WebSizeInfoImpl extends BaseService implements IWebSizeInfoService {


    @Autowired
    private SettingDao settingsDao;

    @Autowired
    private IdWorker idWorker;

    @Override
    public ResponseResult getWebSizeTitle() {
        Setting title = settingsDao.findOneByKey(Constants.Settings.WEB_SIZE_TITLE);
        return ResponseResult.SUCCESS("获取网站title成功").setData(title);

    }

    @Override
    public ResponseResult putWebSizeTitle(String title) {
        if (TextUtils.isEmpty(title)) {
            ResponseResult.FAILED("网站标题不可以为空");
        }
        Setting titleFromDb = settingsDao.findOneByKey(Constants.Settings.WEB_SIZE_TITLE);
        if (titleFromDb == null) {
            titleFromDb = new Setting();
            titleFromDb.setId(idWorker.nextId() + "");
            titleFromDb.setUpdateTime(new Date());
            titleFromDb.setCreateTime(new Date());
            titleFromDb.setKey(Constants.Settings.WEB_SIZE_TITLE);
        }
        titleFromDb.setValue(title);
        settingsDao.save(titleFromDb);
        return ResponseResult.SUCCESS("网站title更新成功");
    }

    @Override
    public ResponseResult getSeoInfo() {
        Setting description = settingsDao.findOneByKey(Constants.Settings.WEB_SIZE_DESCRIPTION);
        Setting keywords = settingsDao.findOneByKey(Constants.Settings.WEB_SIZE_KEYWORDS);
        Map<String, String> result = new HashMap<>();
        result.put(description.getKey(), description.getValue());
        result.put(keywords.getKey(), keywords.getValue());
        return ResponseResult.SUCCESS("获取SEO信息成功").setData(result);

    }

    @Override
    public ResponseResult PutSeoInfo(String keywords, String description) {
        //判断
        if (TextUtils.isEmpty(keywords)) {
            return ResponseResult.FAILED("关键字不可以为空");
        }
        if (TextUtils.isEmpty(description)) {
            return ResponseResult.FAILED("网站描述不能为空");
        }
        Setting descriptionFromDb = settingsDao.findOneByKey(Constants.Settings.WEB_SIZE_DESCRIPTION);
        if (descriptionFromDb == null) {
            descriptionFromDb = new Setting();
            descriptionFromDb.setId(idWorker.nextId() + "");
            descriptionFromDb.setUpdateTime(new Date());
            descriptionFromDb.setCreateTime(new Date());
            descriptionFromDb.setKey(Constants.Settings.WEB_SIZE_DESCRIPTION);
        }
        descriptionFromDb.setValue(description);
        settingsDao.save(descriptionFromDb);
        Setting keywordsFromDb = settingsDao.findOneByKey(Constants.Settings.WEB_SIZE_KEYWORDS);
        if (keywordsFromDb == null) {
            keywordsFromDb = new Setting();
            keywordsFromDb.setId(idWorker.nextId() + "");
            keywordsFromDb.setUpdateTime(new Date());
            keywordsFromDb.setCreateTime(new Date());
            keywordsFromDb.setKey(Constants.Settings.WEB_SIZE_KEYWORDS);
        }
        keywordsFromDb.setValue(keywords);
        settingsDao.save(keywordsFromDb);
        return ResponseResult.SUCCESS("更新SEO信息成功.");
    }

    /**
     * 这个是全网站的访问量，要做的紫衣殿，还带分来源
     * 这里只是统计浏览量，只统计文章的浏览量，提供一个浏览量的统计接口（页面级的）
     *
     * @return 浏览量
     */
    @Override
    public ResponseResult getWebSizeViewCount() {
        //先从redis里拿出来
        String viewCountStr = (String) redisUtils.get(Constants.Settings.WEB_SIZE_VIEW_COUNT);
        Setting viewCount = settingsDao.findOneByKey(Constants.Settings.WEB_SIZE_VIEW_COUNT);
        if (viewCount == null) {
            viewCount = this.initViewItem();
            settingsDao.save(viewCount);
        }
        if (TextUtils.isEmpty(viewCountStr)) {
            viewCountStr = viewCount.getValue();
            redisUtils.set(Constants.Settings.WEB_SIZE_VIEW_COUNT,viewCountStr);
        }else {
            //把redis里的更新到数据库
            viewCount.setValue(viewCountStr);
            settingsDao.save(viewCount);
        }

        Map<String, Integer> result = new HashMap<>();
        result.put(viewCount.getKey(), Integer.valueOf(viewCount.getValue()));
        return ResponseResult.SUCCESS("获取网站浏览量成功.").setData(result);
    }

    private Setting initViewItem(){
       Setting viewCount = new Setting();
        viewCount.setId(idWorker.nextId() + "");
        viewCount.setKey(Constants.Settings.WEB_SIZE_VIEW_COUNT);
        viewCount.setUpdateTime(new Date());
        viewCount.setCreateTime(new Date());
        viewCount.setValue("1");
        return viewCount;
    }

    @Autowired
    private RedisUtils redisUtils;

    /**
     * 1.并发量
     * 2.过滤相同ip，id
     * 3.防止攻击，比如说太频繁的访问，就提示请稍后访问
     */
    @Override
    public void updateViewCount() {
        //redis的更新时机
        Object viewCount = redisUtils.get(Constants.Settings.WEB_SIZE_VIEW_COUNT);
        if (viewCount == null) {
            Setting setting = settingsDao.findOneByKey(Constants.Settings.WEB_SIZE_VIEW_COUNT);
            if (setting == null) {
                setting = this.initViewItem();
                settingsDao.save(setting);
            }
            redisUtils.set(Constants.Settings.WEB_SIZE_VIEW_COUNT, setting.getValue());
        }else {
            //数字自增
            redisUtils.incr(Constants.Settings.WEB_SIZE_VIEW_COUNT, 1);
        }

    }
}
